using System;
using System.Collections.Generic;
using System.Text;
using QLPMT_DTO;
using QLPMT_DAO;

namespace QLPMT_BUS
{
   public class DonViBUS
    {
       public List<DonVi> GetAll()
       {
           return new DonViDAO().GetAll();
       }
       // Lay ma don vi
       public string GetMaDonVi()
       {
           List<DonVi> list = new List<DonVi>();
           list = new DonViDAO().GetAll();
           DonVi temp = new DonVi();
           foreach (DonVi X in list)
           {
               temp.Madonvi = X.Madonvi;
           }
           string ma = "";
           if (temp.Madonvi != null)
           {
               char[] chuoi = temp.Madonvi.ToCharArray(2, temp.Madonvi.Length - 2);
               string chuoima = "";
               foreach (char charX in chuoi)
               {
                   chuoima = chuoima + charX;
               }
               int soluongdonvi = int.Parse(chuoima) + 1;
               ma = "DV" + soluongdonvi.ToString();
           }
           else
               ma = "DV1";
           return ma;
       }
       // them don vi moi
       public Boolean InsertDonVi(string ma, string ten)
       {
           try
           {
           DonVi dto = new DonVi(ma, ten);
                   new DonViDAO().ThemDonViMoi(dto);
             
           }
           catch
           {
               return false;
           }
           return true;
       }
       public void SuaDV(string ma, string ten)
       {
           DonViDAO dao = new DonViDAO();
           DonVi dto = new DonVi(ma, ten);
           dao.SuaDonVi(dto);
       }
    }
}
